package com.example.myapplication.vision.idcard.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;

import com.example.myapplication.R;

/**
 * Create By TreeRoot
 * Create Time: 2024/01/24 09:45
 * Class Name: VisionMainActivity
 * Description:
 *
 * @author TreeRoot
 */
public class VisionReplaceActivity extends AppCompatActivity {

    /**
     * 相机Fragment
     */
    private VisionCameraFragment mCameraFragment;

    /**
     * 需要的权限
     */
    private static final String[] NEEDED_PERMISSIONS = new String[]{
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
         * 设置布局
         */
        setContentView(R.layout.vision_replace_activity);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (permissionGranted()) {
            initCameraFragment();
        }
    }

    /**
     * 初始化相机Fragment
     */
    private void initCameraFragment() {
        if (mCameraFragment == null) {
            mCameraFragment = new VisionCameraFragment();
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_container, mCameraFragment)
                    .commit();
        }
    }

    /**
     * 初始化、权限检测申请
     */
    private boolean permissionGranted() {
        boolean allGranted = true;
        for (String needPermission : NEEDED_PERMISSIONS) {
            allGranted &= ContextCompat.checkSelfPermission(this, needPermission) == PackageManager.PERMISSION_GRANTED;
        }
        if (!allGranted) {
            ActivityCompat.requestPermissions(this, NEEDED_PERMISSIONS, 2);
        }
        return allGranted;
    }
}